# -*- coding: utf-8 -*-
import scrapy
from copy import deepcopy
from redis import Redis


class Dangdangbook0Spider(scrapy.Spider):
    name = 'dangdangbook0'
    allowed_domains = ['dangdang.com']
    start_urls = ['http://book.dangdang.com/']
    conn = Redis(host='127.0.0.1',port=6379)

    def parse(self, response):
        div_list = response.xpath("//div[@class='con flq_body']/div")
        for div in div_list:
            item = {}
            #大分类
            item["b_cate"] = div.xpath("./dl/dt//text()").extract()
            item["b_cate"] = [i.strip() for i in item['b_cate'] if len(i.strip()) > 0]
            item["b_cate"] = '/'.join(item['b_cate'])

            #print(item)
            #中分类
            dl_list = div.xpath(".//dl[@class='inner_dl']")
            for dl in dl_list:
                item["m_cate"] = dl.xpath("./dt//text()").extract()
                item["m_cate"] = [i.strip() for i in item['m_cate'] if len(i.strip()) > 0]
                item["m_cate"] = '/'.join(item['m_cate'])
                #小分类
                a_list = dl.xpath("./dd/a")
                for a in a_list:
                    item["s_href"] = a.xpath("./@href").extract_first()
                    item["s_cate"] = a.xpath("./@title").extract()
                    item["s_cate"] = [i.strip() for i in item['s_cate'] if len(i.strip()) > 0]
                    item["s_cate"] = '/'.join(item['s_cate'])
                    yield scrapy.Request(
                        url=item['s_href'],
                        callback=self.parse_book_list,
                        meta = {"item":deepcopy(item)}
                    )

    def parse_book_list(self,response):
        item = response.meta.get('item')
        li_list = response.xpath("//ul[@class='bigimg']/li")
        #print(li_list)
        for li in li_list:
            item["book_img"] = li.xpath("./a[@class='pic']/img/@src").extract_first()
            if item["book_img"] == 'images/model/guan/url_none.png':
                item["book_img"] = li.xpath("./a[@class='pic']/img/@data-original").extract_first()
            item["book_name"] = li.xpath("./a[@class='pic']/@title").extract_first()
            item["book_price"] = li.xpath(".//span[@class='search_now_price']/text()").extract_first()
            item["book_price"] = str(item['book_price'])
        if 'book_img' not in item:
            item["book_img"] = '分类页面或此条数据无此信息'
        if 'book_name' not in item:
            item["book_name"] = '分类页面或此条数据无此信息'
        if 'book_price' not in item:
            item["book_price"] = '分类页面或此条数据无此信息'

        print(item)
        yield item